//
//  TTDianpuImageViewCell.m
//  TaoTuo
//
//  Created by admin on 2019/11/6.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTDianpuImageViewCell.h"
#import "TTDianpuImageCellModel.h"
#import "TTDianpuImageCollectionViewCell.h"
#import "YBImageBrowser.h"

static NSString *cellID = @"collection_cell_id";

@interface TTDianpuImageViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIButton *arrowBtn;

@end

@implementation TTDianpuImageViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self titleLabel];
        [self collectionView];
        [self arrowBtn];
        
    }
    return self;
}

// 点击箭头
- (void)clickArrowBtn
{
    if ([self.delegate respondsToSelector:@selector(clickImgCell:model:)]) {
        [self.delegate clickImgCell:self model:self.model];
    }
}

- (void)setModel:(TTDianpuImageCellModel *)model
{
    _model = model;
    self.titleLabel.text = model.title;
    if (model.fontSize > 0) {
        self.titleLabel.font = [UIFont systemFontOfSize:model.fontSize];
    }
    [self.collectionView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.model.imageUrlArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TTDianpuImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    cell.urlString = self.model.imageUrlArray[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    if ([self.delegate respondsToSelector:@selector(clickImgCell:model:)]) {
//        [self.delegate clickImgCell:self model:self.model];
//    }
    
    @autoreleasepool {
        NSMutableArray *imgs = [NSMutableArray array];
        for (NSString *urlString in self.model.imageUrlArray) {
            YBIBImageData *imageData = [YBIBImageData new];
            imageData.imageURL = [NSURL URLWithString:urlString];
            [imgs addObject:imageData];
        }
        
        YBImageBrowser *browser = [YBImageBrowser new];
        browser.dataSourceArray = imgs;
        browser.currentPage = indexPath.row;
        [browser show];
    }
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:11];
        [self.contentView addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(14);
            make.left.mas_equalTo(15);
        }];
    }
    return _titleLabel;
}

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(75, 75);
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = 7;
//        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 10);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[TTDianpuImageCollectionViewCell class] forCellWithReuseIdentifier:cellID];
        [self.contentView addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom).mas_offset(15);
            make.left.mas_equalTo(15);
            make.height.mas_equalTo(75);
            make.right.equalTo(self.contentView).mas_offset(-30);
        }];
    }
    return _collectionView;
}

- (UIButton *)arrowBtn
{
    if (!_arrowBtn) {
        _arrowBtn = [UIButton createButtonWithImage:[UIImage imageNamed:@"order_right_arrow"] target:self action:@selector(clickArrowBtn)];
        [self.contentView addSubview:_arrowBtn];
        [_arrowBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView);
            make.width.height.mas_equalTo(30);
            make.centerY.equalTo(self.collectionView);
        }];
    }
    return _arrowBtn;
}

@end
